package com.linjiahao.nettyBus.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName NettyServerHandler
 * @Description: TODO
 * @Author linjiahao
 * @Date 2022/3/22
 **/
@Slf4j
public class NettyServerHandler extends SimpleChannelInboundHandler<Object> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("netty:TCP客户端与服务端连接开始");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("netty:TCP客户端与服务端连接关闭");
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
        log.info("netty:TCP信息接收完毕");
        ctx.channel().read();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
        log.info("netty:当前连接出现异常",cause.getMessage());
    }

    @Override
    public void channelRead0(ChannelHandlerContext channelHandlerContext, Object info) throws Exception {
        log.info("来自TCP客户端" + channelHandlerContext.channel().remoteAddress() + "的信息: " + info.toString());
        //读取
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        super.handlerAdded(ctx);
        log.info("netty:TCP客户端与服务端连接开始");
    }
}
